void main() {
    vec2 fragCoord = gl_FragCoord.xy;
    vec2 uv = (fragCoord*2.0-iResolution)/min(iResolution.x, iResolution.y);
    uv.y *= -1.0;

    Ray r0 = Ray(vec3(0.0, 0.0, 0.0), normalize(vec3(uv, -1.0)));

    Intersect hit = march(r0);

    if(hit==miss){
        fragColor = vec4(miss.material.color, 1.0);
    }else{
        Ray r1 = Ray(r0.origin+hit.len*r0.dir+2.0*MIN_DISTANCE*hit.normal, lit.dir);
        Intersect shadow = march(r1);
        if(shadow != miss){
            fragColor = vec4(vec3(0), 1.0);
        } else{
            // 微表面法线
            vec3 microNormal = normalize( (-r0.dir) + lit.dir);
            vec3 screenNormal = vec3(0,0,1);
            float k = lit.h*fresnel(hit.material.r0, dot(lit.dir, microNormal))*dot(hit.normal, lit.dir)/dot(hit.normal, -r0.dir)*dot(-r0.dir, screenNormal)/3.1415926;
            k *= (0.75 + pow(dot(hit.normal, microNormal), 50));
            vec3 litColor = k*hit.material.color;
            fragColor = vec4(litColor, 1.0);
        }
    }
}
